import logging
from typing import Optional

class Logger:
    """
    日志管理单例类，负责日志配置和提供日志器
    """
    _instance: Optional['Logger'] = None
    
    def __new__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance._initialized = False
        return cls._instance
    
    def __init__(self, level: int = logging.INFO, format: str = None):
        """
        初始化日志配置（只会执行一次）
        
        :param level: 日志级别
        :param format: 日志格式
        """
        if getattr(self, '_initialized', False):
            return
            
        self._initialized = True
        self._level = level
        self._format = format or '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
        
        # 基础配置
        logging.basicConfig(
            level=self._level,
            format=self._format
        )
    
    @classmethod
    def get_logger(cls, name: str = None) -> logging.Logger:
        """
        获取日志器
        
        :param name: 日志器名称，通常使用 __name__
        :return: 配置好的日志器实例
        """
        # 确保单例已初始化（使用默认配置）
        if cls._instance is None:
            cls()
        return logging.getLogger(name)